package com.tulingxueyuan.mall.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductAttribute;
import com.tulingxueyuan.mall.modules.pms.mapper.PmsProductAttributeMapper;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductAttributeCategory;
import com.tulingxueyuan.mall.dto.RelationAttrInfoDto;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductAttributeCategoryService;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductAttributeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 商品属性参数表 服务实现类
 * </p>
 *
 * @author SiXiaoLong
 * @since 2022-10-13
 */
@Service
public class PmsProductAttributeServiceImpl extends ServiceImpl<PmsProductAttributeMapper, PmsProductAttribute> implements PmsProductAttributeService {
    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    @Autowired
    PmsProductAttributeMapper pmsProductAttributeMapper;

    @Override
    public Page getList(Long productAttributeCategoryId, Integer pageNum, Integer pageSize, Integer type) {
        Page<PmsProductAttribute> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PmsProductAttribute> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProductAttribute::getProductAttributeCategoryId,productAttributeCategoryId);
        lqw.eq(PmsProductAttribute::getType,type);
        lqw.orderByDesc(PmsProductAttribute::getSort);
        return this.page(page,lqw);
    }

    @Override
    @Transactional
    public boolean create(PmsProductAttribute pmsProductAttribute) {
        boolean save = this.save(pmsProductAttribute);
        if (save){
//            :更新对应属性、参数的数量
            LambdaUpdateWrapper<PmsProductAttributeCategory> uw = new LambdaUpdateWrapper<>();
            uw.eq(PmsProductAttributeCategory::getId,pmsProductAttribute.getProductAttributeCategoryId());
//            属性
            if (pmsProductAttribute.getType() == 0){
                uw.setSql("attribute_count=attribute_count+1");
//            参数
            }else if (pmsProductAttribute.getType() == 1){
                uw.setSql("param_count=param_count+1");
            }
            pmsProductAttributeCategoryService.update(uw);
        }
        return save;
    }

    /**
     * 批量删除
     */
    @Override
    @Transactional
    public boolean delete(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return false;
        }
        PmsProductAttribute pmsProductAttribute = null;
        for (String id : ids) {
//            查询到了商品属性
            pmsProductAttribute = this.getById(id);
            if (pmsProductAttribute!=null){
                break;
            }
        }
        //        获取受影响的行数  对应的就是所删除的行数
        int deleteBatchIds = pmsProductAttributeMapper.deleteBatchIds(ids);
        if(deleteBatchIds>0 && pmsProductAttribute!=null){
            LambdaUpdateWrapper<PmsProductAttributeCategory> luw = new LambdaUpdateWrapper<>();
            if (pmsProductAttribute.getType()==0){
                luw.setSql("attribute_count=attribute_count-"+deleteBatchIds);
            }else if (pmsProductAttribute.getType()==1){
                luw.setSql("param_count=param_count-"+deleteBatchIds);
            }
            luw.eq(PmsProductAttributeCategory::getId,pmsProductAttribute.getProductAttributeCategoryId());
            pmsProductAttributeCategoryService.update(luw);
        }
        return deleteBatchIds > 0;
    }

    @Override
    public List<RelationAttrInfoDto> getRelationAttrInfoById(Long id) {
        return pmsProductAttributeMapper.getRelationAttrInfoById(id);
    }
}
